//
//  TDViewController.m
//  TotalDemo
//
//  Created by gujian on 16/5/30.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "TDViewController.h"
#import "MessageViewController.h"

@interface TDViewController ()<MessageViewControllerDelegate>

@property (nonatomic, strong) UIViewController *tdModalViewController;
@property (nonatomic, strong) UIButton *backBtn;

@end

@implementation TDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initSubViews];
}

- (void)initSubViews
{
    CGFloat btnWidth = 100;
    CGFloat btnHeight = 34.0;
    
    CGFloat offY = 40.0;
    
    UIButton *showBtn = [TDViewUtils createCustomButton];
    showBtn.frame = CGRectMake((SCREEN_WIDTH-btnWidth)/2.0, offY, btnWidth, btnHeight);
    [showBtn addTarget:self action:@selector(showAction:) forControlEvents:UIControlEventTouchUpInside];
    [showBtn setTitle:@"显示" forState:UIControlStateNormal];
    [self.view addSubview:showBtn];
    [self.view setBackgroundColor:[UIColor randomColor]];
    

}

- (void)initRightButton
{
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"隐藏" style:UIBarButtonItemStylePlain target:self action:@selector(hideAction:)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

#pragma mark - Target Action

- (void)showAction:(UIButton *)btn
{
    MessageViewController *messageViewController = [[MessageViewController alloc] init];
    messageViewController.delegate = self;
    messageViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
//    [self presentTDModalViewController:messageViewController];
    
    //1
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:messageViewController];
//    nav.navigationBar.hidden = YES;
//    [self presentViewController:nav animated:YES completion:nil];
    
    //2
    self.navigationController.navigationBar.hidden = YES;
    [self.navigationController pushViewController:messageViewController animated:YES];
}

- (void)hideAction:(id)sender
{
    [self dismissTdViewController];
}


#pragma mark - Private Methods
- (void)presentTDModalViewController:(UIViewController *)modalViewController
{
    if (!self.tdModalViewController && modalViewController ) {
        self.tdModalViewController = modalViewController;
        
        [self addChildViewController:modalViewController];
        
        [modalViewController beginAppearanceTransition:YES animated:YES];
        modalViewController.view.center = CGPointMake(CGRectGetWidth(self.view.frame)/2.0, CGRectGetHeight(self.view.frame)/2.0);
        [self.view addSubview:modalViewController.view];
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        if (keyWindow != nil) {
            [keyWindow addSubview:modalViewController.view];
        }
        
        //动画
        modalViewController.view.frame = CGRectMake(0, SCREEN_HEIGHT, self.view.width, self.view.height+64);
        modalViewController.view.alpha = 0.0;
        
        [UIView animateWithDuration:0.3 animations:^{
            modalViewController.view.frame = CGRectMake(0, 0, self.view.width, self.view.height+64);
            modalViewController.view.alpha = 1.0;
        } completion:^(BOOL finished) {
            [modalViewController endAppearanceTransition];
            [modalViewController didMoveToParentViewController:self];
        }];
        
        
    }
}

- (void)dismissTdViewController
{
    if (self.tdModalViewController) {
        [self.tdModalViewController willMoveToParentViewController:nil];
        [self.tdModalViewController beginAppearanceTransition:NO animated:YES];
        
        //动画
        
        [UIView animateWithDuration:0.3 animations:^{
            self.tdModalViewController.view.frame = CGRectMake(0, SCREEN_HEIGHT, self.view.width, self.view.height+64);
            self.tdModalViewController.view.alpha = 0.0;
        } completion:^(BOOL finished) {
            
            [self.tdModalViewController.view removeFromSuperview];
            self.tdModalViewController.view.alpha = 1.0;
            [self.tdModalViewController endAppearanceTransition];
            [self.tdModalViewController removeFromParentViewController];
            self.tdModalViewController = nil;
        }];
        
        
    }
}

#pragma mark - MessageViewController delegate

- (void)shouldHideMessageViewController:(UIViewController *)messageViewController
{
    [self dismissTdViewController];
}

@end
